package com.msb.mall.product.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.CategoryService;
import com.msb.mall.product.vo.Catalog2VO;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/","/home","/index"})
    public String index(Model model){
        //查询出所有的一级分类的信息
        List<CategoryEntity> list = categoryService.getLevelCategory1();
        model.addAttribute("categorys",list);
        //classPath:templates
        //.html
        return "index";
    }

    //index/catalog.json
    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<Catalog2VO>> getCatalog2JSON(){
        Map<String, List<Catalog2VO>> map = categoryService.getCatalog2JSON();
        return map;
    }

    /**
     * 1.锁会自动续期，如果业务时间超长，运行期间Redisson会自动给锁重新添加30s，不用担心业务时间，锁自动释放而造成的安全问题
     * 2.加锁的业务只要执行完成，那么就不会给当前的锁续期，即使我们不去自动的释放锁，锁在默认30s之后也会自动的删除
     *
     * 如果我们制定了锁的过期时间，那么源码中会直接帮我们创建一个过期时间是制定值的锁，时间到期后就会直接把改锁删除
     * 如果我们没有制定过期时间，那么在执行的时候首先会创建一把锁且过期时间是30s然后会创建异步任务，每隔10s执行一次任务来续期
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        RLock myLock = redissonClient.getLock("myLock");
        //加锁
        //myLock.lock();
        //获取锁，并且给定的过期时期时间为10秒 问题？ 业务如果时间超过了10s，会不会自动续期
        //通过效果演示我们可以发现，制定了过期时间后那么自动续期就不会生效了
        // ,这时我们就需要注意设置的过期时间一定要满足我们的业务场景
        myLock.lock(10, TimeUnit.SECONDS);
        try {
            System.out.println("加锁成功...业务处理...." + Thread.currentThread().getName());
            Thread.sleep(30000);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("释放锁成功..." + Thread.currentThread().getName());
            //释放锁
            myLock.unlock();
        }
        return "hello";
    }

    @GetMapping("/writer")
    @ResponseBody
    public String writerValue(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        //加写锁
        RLock wLock = readWriteLock.writeLock();
        String uuid = null;
        wLock.lock();//加写锁
        try {
            uuid = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("msg",uuid);
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //释放锁
            wLock.unlock();
        }
        return uuid;
    }

    @GetMapping("/reader")
    @ResponseBody
    public String readValue(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        //加读锁
        RLock rLock = readWriteLock.readLock();
        String uuid = null;
        rLock.lock();
        try {
            uuid = stringRedisTemplate.opsForValue().get("msg");
        }finally {
            //释放锁
            rLock.unlock();
        }
        return uuid;
    }

    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor(){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        try {
            door.await();//等待数量为0
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "关门熄灯...";
    }

    @GetMapping("/goHome/{id}")
    @ResponseBody
    public String goHome(@PathVariable Long id){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();//递减的操作
        return id + "下班走人";
    }

    @GetMapping("/park")
    @ResponseBody
    public String park(){
        RSemaphore park = redissonClient.getSemaphore("park");
        try {
            park.acquire();//获取信息
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "停车成功";
    }

    @GetMapping("/release")
    @ResponseBody
    public String release(){
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();
        return "释放了一个车位";
    }
}
